# -*- coding: utf-8 -*-
# coding: UTF-8
from scrapy.spiders import CrawlSpider
from scrapy.selector import Selector
from scrapy.http import Request

from ..items import Uuu833Item


class uuu833Spider(CrawlSpider):
    name = 'uuu833Spider'
    host = 'https://www.uuu833.com/%s'
    start_urls = ['htm/piclist1/1.htm']

    def start_requests(self):
        for db_type in self.start_urls:
            yield Request(url=self.host % db_type,
                          callback=self.parse_db_key)

    def parse_db_key(self, response):
        sel = Selector(response)
        lists = sel.xpath('//ul[@class="textList"]/li/a')
        for li in lists:
            url = li.xpath('@href').extract()
            yield Request(url=self.host % url[0], callback=self.parse_ph_info)

        next_page = sel.xpath('//div[@class="pageList"]/a[last()-1]/@href').extract()[0]
        if next_page:
            yield Request(url=self.host % next_page, callback=self.parse_db_key)

    def parse_ph_info(self, response):
        item = Uuu833Item()
        sel = Selector(response)

        name = sel.xpath('///html/head/title/text()').extract()[0]

        imgs = sel.xpath('//div[@class="picContent"]/img/@src').extract()

        item['name'] = name.split('-')[0]
        item['image_urls'] = imgs
        yield item
